import type { Router } from 'vue-router'
import { defineStore } from 'pinia'
import { cacheEnum } from '~/enums/cache'
import CryptoJS from 'crypto-js'
import toolsUtil from '~/utils/tools'
import loginApi from '~/api/login'
import userApi from '~/api/user'

interface UserSate {
    visitUid: string | undefined | null;
    token: string | undefined | null;
    users: UserCenterResponse;
}

const useUserStore = defineStore('userStore', {
    state: (): UserSate => {
        const TOKEN = useCookie(cacheEnum.TOKEN_KEY, {
            secure: true,
            sameSite: 'strict'
        })
        const visitUid = useCookie(cacheEnum.IDENTITY_KEY, {
            secure: true,
            sameSite: 'strict'
        })
        return {
            visitUid: visitUid.value,
            token: TOKEN.value,
            users: {} as UserCenterResponse
        }
    },
    getters: {
        isLogin: (state: UserSate) => !!state.token,
        user: (state: UserSate) => state.users
    },
    actions: {
        /**
         * 获取访问UID
         */
        getVisitUid(): string {
            const visitUid = useCookie(cacheEnum.IDENTITY_KEY, {
                secure: true,
                sameSite: 'strict'
            })
            if (!visitUid.value) {
                const timestamp: number = Date.now()
                const uniqueId: string = toolsUtil.uniqueId()
                const md5 = CryptoJS.MD5(timestamp + uniqueId).toString()
                const cok = useCookie(cacheEnum.IDENTITY_KEY, {
                    secure: true,
                    sameSite: 'strict'
                })
                this.visitUid = md5
                cok.value = md5
            }
            return String(this.visitUid)
        },
        /**
         * 获取用户
         */
        async getUser(): Promise<UserCenterResponse> {
            try {
                this.users = await userApi.center()
                return this.users
            } catch {
                await this.logout()
                return {} as UserCenterResponse
            }
        },
        /**
         * 登录系统
         */
        login(token: string): void {
            const oneYear: number = 360 * 24 * 60 * 60 * 1000
            const TOKEN = useCookie(cacheEnum.TOKEN_KEY, {
                expires: new Date(Date.now() + oneYear),
                secure: true,
                sameSite: 'strict'
            })

            this.token = token
            TOKEN.value = token
        },
        /**
         * 退出系统
         */
        async logout(): Promise<any> {
            const router: Router = useRouter()
            return new Promise((resolve, reject): void => {
                loginApi.logout().then((data: any): void => {
                    const TOKEN = useCookie(cacheEnum.TOKEN_KEY, {
                        secure: true,
                        sameSite: 'strict'
                    })
                    this.token = ''
                    this.users = {} as UserCenterResponse
                    TOKEN.value = null
                    router.push('/')
                    resolve(data)
                }).catch((error: any): void => {
                    reject(error)
                })
            })
        }
    }
})

export default useUserStore
